package March16;

//import junit.framework.Assert;

import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import cucumber.api.java.After;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;


public class StepFile1 {
	
	protected WebDriver driver;
	
	@Before
	public void launchBrowser(){
		
		driver = new FirefoxDriver();
		driver.manage().window().maximize();
		//driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		driver.get("http://www.gmail.com");
	}
	
	@After
	public void closeBrowser(){
		driver.quit();
	}
	
	//@SuppressWarnings("deprecation")
	@Given("^user is on login page$")
	public void user_is_on_login_page() throws Throwable {
	    
	    //throw new PendingException();
	    String exp_pageTitle = "Gmail";
	    String act_pateTitle = driver.getTitle();
	    //Assert.assertTrue(pateTitle, true);
	    Assert.assertEquals(exp_pageTitle, act_pateTitle);
	    //Assert.assertTrue(driver.getTitle().contains("Gmail"));
	    System.out.println("User is on Gmail login page");
	}
	
	@When("^user enter userID \"([^\"]*)\"$")
	public void user_enter_userID(String user) throws Throwable {
	    // Express the Regexp above with the code you wish you had
	    //throw new PendingException();
		driver.findElement(By.id("Email")).sendKeys(user);
	}
	/*@When("^user enter userID$")
	public void user_enter_userID() throws Throwable {
	    //System.out.println("Entring userid");
	    //throw new PendingException();
		driver.findElement(By.id("Email")).sendKeys("it.tyagi");
	}*/
	
	@When("^user enter passwd \"([^\"]*)\"$")
	public void user_enter_passwd(String passwd) throws Throwable {
	    // Express the Regexp above with the code you wish you had
	    //throw new PendingException();
		driver.findElement(By.id("Passwd")).sendKeys(passwd);
		if(driver.findElement(By.id("PersistentCookie")).isSelected()){
			
			driver.findElement(By.id("PersistentCookie")).click();
		}
	}

	/*@When("^user enter passwd$")
	public void user_enter_passwd() throws Throwable {
	    //System.out.println("Entring passwd");
	    //throw new PendingException();
		driver.findElement(By.id("Passwd")).sendKeys("");
		if(driver.findElement(By.id("PersistentCookie")).isSelected()){
			
			driver.findElement(By.id("PersistentCookie")).click();
		}
	}*/

	@When("^click on login button$")
	public void click_on_login_button() throws Throwable {
	    //System.out.println("Clicking on SignIN button");
	    //throw new PendingException();
		driver.findElement(By.id("signIn")).click();
	}

	//@SuppressWarnings("deprecation")
	@Then("^user should be login$")
	public void user_should_be_login() throws Throwable {
	    Thread.sleep(9000);
		try{
			Assert.assertTrue(driver.getTitle().contains("Inbox"));
		}
		catch(Exception e){
			System.out.println("Error");
		}
		System.out.println("Now user is on Inbox page");
	    //throw new PendingException();
	}

	/*@Then("^he/she count unread emails$")
	public void he_she_count_unread_emails() throws Throwable {
	    //System.out.println("User is checking his/her emails");
	    //throw new PendingException();
		Thread.sleep(15000);
		
		System.out.println("*****************************************************");
		System.out.println(driver.getPageSource());
		System.out.println("*****************************************************");
		
		List<WebElement>myL = driver.findElements(By.tagName("iframe"));
		System.out.println("No. of iframe is : " + myL.size());
		//String countEmail = driver.findElement(By.xpath("//a[@href ='https://mail.google.com/mail/u/0/#inbox']")).getText();
		 String countEmail = driver.findElement(By.xpath("//a[@class ='J-Ke n0']")).getText();
		  
		System.out.println("Emails in your Inbox are : " + countEmail);
		
		//driver.findElement(By.xpath("//div[text() = 'COMPOSE']")).click();
		
		
	}*/

}
